package cn.common.config;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;
/**
 * Copyright
 *
 * @Author WuHuang
 * @Date 2022/12/21
 */

@Configuration
@Import({MyRegistrarBean.class})
public class MyRegistrarBean implements ImportBeanDefinitionRegistrar, BeanFactoryAware {
    private ConfigurableListableBeanFactory beanFactory;
    private static BeanDefinitionRegistry registry;

    public MyRegistrarBean() {
    }

    public void setBeanFactory(BeanFactory factoryBean) throws BeansException {
        if (factoryBean instanceof ConfigurableListableBeanFactory) {
            this.beanFactory = (ConfigurableListableBeanFactory)factoryBean;
        }

    }

    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry beanDefinitionRegistry) {
        if (this.beanFactory != null) {
            registry = beanDefinitionRegistry;
        }
    }

    public ConfigurableListableBeanFactory getBeanFactory() {
        return this.beanFactory;
    }

    public static BeanDefinitionRegistry getRegistry() {
        return registry;
    }
}
